//
//  XKLShowPictureViewController.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/5/7.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLShowPictureViewController.h"
#import <UIImageView+WebCache.h>
#import "XKLEessenceItem.h"
#import "XKLProgress.h"
#import <SVProgressHUD.h>


@interface XKLShowPictureViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *pictureView;
@property (weak, nonatomic) IBOutlet XKLProgress *progressView;

@property (weak,nonatomic)UIImageView *pictureImageView;
@end

@implementation XKLShowPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setImageView];
    
    
}

//图片显示
-(void)setImageView{
    UIImageView *imageView = [[UIImageView alloc] init];
    [self.pictureView addSubview:imageView];
    self.pictureImageView=imageView;
    //计算大小
    CGFloat imageW = ScreenW;
    CGFloat imageH =imageW * self.item.height /self.item.width;
    
    if (imageH>ScreenH) {

        imageView.frame = CGRectMake(0, 0, imageW, imageH);
        self.pictureView.contentSize = CGSizeMake(imageW, imageH);
        self.pictureView.minimumZoomScale = 1;
        self.pictureView.maximumZoomScale = self.item.height / imageH;
     
        
    }else{
        imageView.bounds = CGRectMake(0, 0,imageW, imageH);
        imageView.center = CGPointMake(ScreenW * 0.5, ScreenH * 0.5);

    }
    
    [self.progressView setProgress:self.item.imageProgress animated:NO];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.item.image1] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        [self.progressView setProgress:(CGFloat)receivedSize /expectedSize  animated:NO];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    } ];
    
    
    
}

//-(void)viewDidLayoutSubviews{
//    [super viewDidLayoutSubviews];
//    
//    NSLog(@"%@",NSStringFromCGSize(self.pictureView.contentSize));
//    if (self.pictureImageView.height>ScreenH) {
//        
//        self.pictureImageView.center = CGPointMake(self.pictureView.contentSize.width * 0.5, self.pictureView.contentSize.height * 0.5);
//        
//    }else{
//        self.pictureImageView.center = CGPointMake(self.pictureView.width * 0.5, self.pictureView.height * 0.5);
//        
//    }
//}

#pragma matk -UIScrollViewDelegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.pictureImageView;
}
//
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    
//    CGFloat width = self.pictureView.contentSize.width;
//    CGFloat height = self.pictureView.contentSize.height;
//    
//    if (width<ScreenW) {
//        width=ScreenW;
//    }
//    if (height<ScreenH) {
//        height = ScreenH;
//    }
//    
//    self.pictureImageView.center = CGPointMake(width * 0.5, height * 0.5);
//}

- (IBAction)dismissBut:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    [SVProgressHUD dismiss];
}
- (IBAction)saveBut:(id)sender {
    UIImageWriteToSavedPhotosAlbum(self.pictureImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
}

- (IBAction)zhuanFaBut:(id)sender {
    
    XKLLogFunc;
}

//图片存储之后调用
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error) {
        NSLog(@"%@",error);
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    }else {
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
